﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using BestTrader.Cms.Core.Helper;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Services;
using BestTrader.Cms.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace BestTrader.Cms.UserCenter.Controllers
{
    /// <summary>
    /// 风软历史出入金记录
    /// </summary>
    [Authorize]
    public class HistoryCashController : Controller
    {
        public String UserId { get { return User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value; } }
        public String RoleId { get { return User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; } }
        private readonly IHistory_CashChangesService _service;
        private readonly IManagerService _managerService;


        public HistoryCashController(IHistory_CashChangesService service, IManagerService managerService)
        {
            this._service = service;
            _managerService = managerService;
        }

        /// <summary>
        /// 主界面
        /// </summary>
        public IActionResult Index()
        {
            ViewData["AgentManagers"] = _managerService.GetSelfAndChildManagerByManagerIdAndRoleId(UserId, RoleId, RoleId == "3");
            return View();
        }

        public string LoadData([FromQuery]HistoryCashRequestModel model)
        {
            var user = User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value;
            var role = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value;
            var data = _service.LoadData(model, user, role);
            var dataTotal = _service.GetTotalHistoryCashByConditions(model, user, role);
            var result = new
            {
                code = data.code,
                data = data.data,
                count = data.count,
                totalData = dataTotal.data
            };
            return JsonHelper.ObjectToJSON(result);
        }
    }
}